package cn.yj.lock;

import java.io.IOException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author 永健
 * @since 2022-02-24 17:35
 */
public class AppTest {

    public static void main(String[] args) throws IOException, InterruptedException {
        APPLock appLock = new APPLock();
        ReentrantLock lock = new ReentrantLock(false);
        int threads = 100;
        CountDownLatch countDownLatch = new CountDownLatch(threads);
        for (int i = 1; i < threads+1; i++) {
            int finalI = i;
            new Thread(() -> {
                try {
                    doIt(appLock, 1);
                    //doIt(lock,1);
                    countDownLatch.countDown();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }, "Thread-" + i).start();
        }
        countDownLatch.await();
        System.out.println(count);
        System.in.read();
    }

    private static volatile int count = 0;

    private static void doIt(APPLock lock, int i) throws InterruptedException {
        try {
            lock.lock("" + i);
            System.out.println(Thread.currentThread().getName() + " 业务处理中.....");
            //Thread.sleep(5000);
            ++count;
        } finally {
            lock.unlock("" + i);
        }
    }

    private static void doIt(ReentrantLock lock, int i) throws InterruptedException {
        try {
            lock.lock();
            System.out.println(Thread.currentThread().getName() + " 业务处理中.....");
            //Thread.sleep(100);
            ++count;
        } finally {
            lock.unlock();
        }
    }

    public synchronized void name(){

    }
}
